package com.jcxy.controller;

import com.jcxy.mapper.AttendanceMapper;
import com.jcxy.pojo.Attendance;
import com.jcxy.pojo.User;
import com.jcxy.service.AttendanceService;
import com.jcxy.util.DateUtil;
import com.jcxy.util.ParamsDto;
import com.jcxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/attendance")
public class AttendanceController {

    @Autowired
    private AttendanceMapper attendanceMapper;
    @Autowired
    private AttendanceService attendanceService;

    /**
     * 获得当天的考勤记录
     * @param request
     * @return
     */
    @RequestMapping("/getTodayAttendance.do")
    @ResponseBody
    public ResponseData getTodayAttendance(HttpServletRequest request){

        //获取登录user
        User user = (User)request.getSession().getAttribute("user");
        //装配考勤实体类
        Attendance attendance = new Attendance();
        attendance.setUser_id(user.getUser_id());
        attendance.setAttendance_date(DateUtil.getCurrDateStr("yyyyMMdd",new Date()));

        List<Attendance> attendanceList = attendanceMapper.queryTodayAttendance(attendance);

        ResponseData ok = ResponseData.ok();
        if (attendanceList != null && attendanceList.size() > 0){
            ok.setData(attendanceList);
            ok.setCode(201);
        }

        return ok;
    }

    /**
     * 打考勤
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/makeAttendance.do")
    public String makeAttendance(HttpServletRequest request)throws Exception{

        //获取考勤类型
        Map dto = ParamsDto.getDto(request.getParameterMap());
        String attendance_type = dto.get("attendance_type").toString();
        //获取登录user
        User user = (User)request.getSession().getAttribute("user");
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        //装配考勤实体类
        Attendance attendance = new Attendance();
        attendance.setAttendance_type(attendance_type);
        attendance.setAttendance_date(DateUtil.getCurrDateStr("yyyyMMdd",date));
        attendance.setAttendance_time(DateUtil.getCurrDateStr("yyyy-MM-dd HH:mm:ss",date));
        attendance.setStatus_id(getAttendanceStatus(attendance_type,calendar));
        attendance.setUser_id(user.getUser_id());

        attendanceService.insertAttendance(attendance);

        return "attendanceManage/attendance";
    }

    /**
     * 获取考勤状态：1-正常，2-迟到，3-早退，4-旷工
     * @param type 考勤类型：0-上班考勤，1-下班考勤
     * @param calendar
     * @return
     */
    public String getAttendanceStatus(String type,Calendar calendar){

        String status;
        int hour = calendar.get(Calendar.HOUR_OF_DAY);//获取小时
        int minute = calendar.get(Calendar.MINUTE);//获取分钟

        if("0".equals(type)){
            //判断上班考勤状态
            if (hour < 9 || (hour == 9 && minute <= 10)){
                //10分钟之内算正常考勤
                status = "1";
            }else if (hour < 10){
                //一小时之内算迟到考勤
                status = "2";
            }else {
                //超过一小时算旷工
                status = "4";
            }

        }else {
            //判断下班考勤状态
            if (hour < 17 || (hour == 17 && minute < 30)){
                //下午5点30分钟之前算早退考勤
                status = "3";
            }else{
                //其他情况都是正常
                status = "1";
            }
        }

        return status;
    }
}
